package com.company.nuwa.common.utils;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * <p>日期工具类</p>
 *
 * @author Lionel Lee
 * @version 1.0: LocalDateTimeUtils.java
 * @date 2021/5/16  10:15
 */
public class LocalDateTimeUtils {
    public static final String DAY              = "天";
    public static final String HOUR             = "时";
    public static final String MINUTES          = "分";
    public static final long   ONE_DAY_MINUTES  = 60 * 24;
    public static final long   ONE_HOUR_MINUTES = 60;

    /**
     * 转xx天xx小时xx分钟
     *
     * @param start
     * @param end
     * @return
     */
    public static String chineseTime(LocalDateTime start, LocalDateTime end) {
        long betweenMinutes = Duration.between(start, end).toMinutes();
        if (betweenMinutes >= ONE_DAY_MINUTES) {
            // day
            int differenceDay = (int) Math.floor(betweenMinutes / ONE_DAY_MINUTES);
            // 剩余分钟
            long remainMinutes = betweenMinutes - differenceDay * ONE_DAY_MINUTES;
            if (remainMinutes >= ONE_HOUR_MINUTES) {
                // 小时
                int differenceHours = (int) Math.floor(remainMinutes / ONE_HOUR_MINUTES);
                // 剩余分钟
                long remainMinutes2 = remainMinutes - differenceHours * ONE_HOUR_MINUTES;
                return differenceDay + DAY + differenceHours + HOUR + remainMinutes2 + MINUTES;
            } else {
                return differenceDay + DAY + remainMinutes + MINUTES;
            }
        } else if (betweenMinutes >= ONE_HOUR_MINUTES) {
            // 小时
            int differenceHours = (int) Math.floor(betweenMinutes / ONE_HOUR_MINUTES);
            // 剩余分钟
            long remainMinutes2 = betweenMinutes - differenceHours * ONE_HOUR_MINUTES;
            return differenceHours + HOUR + remainMinutes2 + MINUTES;

        } else {
            return betweenMinutes + MINUTES;
        }
    }
}
